package com.bean.activiti.activiti.entity.manager;

import com.bean.activiti.entity.SysRole;
import com.bean.activiti.entity.SysUser;
import com.bean.activiti.mapper.SysRoleMapper;
import com.bean.activiti.mapper.SysUserMapper;
import com.bean.activiti.util.ActivitiUtil;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.Picture;
import org.activiti.engine.identity.User;
import org.activiti.engine.identity.UserQuery;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author Gavin Lee
 * @version 1.0
 * @date 2020-9-11 9:23
 * @desc
 */
@Component
public class CustomUserEntityManager implements UserEntityManager,Session {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public void flush() {

    }

    @Override
    public void close() {

    }

    @Override
    public User createNewUser(String s) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void updateUser(User user) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public List<User> findUserByQueryCriteria(UserQueryImpl userQuery, Page page) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public long findUserCountByQueryCriteria(UserQueryImpl userQuery) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public List<Group> findGroupsByUser(String userId) {
        if (userId == null)
            return null;
        SysUser sysUser = sysUserMapper.selectUserByLoginName(userId);
        List<SysRole> sysRoleList = sysRoleMapper.selectRolesByUserId(sysUser.getUserId());
        if (sysRoleList != null && sysRoleList.size() > 0) {
            return ActivitiUtil.toActivitiGroups(sysRoleList);
        }
        return null;
    }

    @Override
    public UserQuery createNewUserQuery() {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public Boolean checkPassword(String s, String s1) {
        SysUser sysUser = sysUserMapper.selectUserByLoginName(s);
        return sysUser.getPassword().equals(s1);
    }

    @Override
    public List<User> findUsersByNativeQuery(Map<String, Object> map, int i, int i1) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public long findUserCountByNativeQuery(Map<String, Object> map) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public boolean isNewUser(User user) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public Picture getUserPicture(String s) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void setUserPicture(String s, Picture picture) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void deletePicture(User user) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public UserEntity create() {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public UserEntity findById(String userId) {
        SysUser sysUser = new SysUser();
        sysUser.setLoginName(userId);
        List<SysUser> sysUserList = sysUserMapper.selectUserList(sysUser);
        if (sysUserList != null && sysUserList.size() > 0) {
            return ActivitiUtil.toActivitiUser(sysUserList.get(0));
        }
        return null;//返回的是activiti的实体类
    }

    @Override
    public void insert(UserEntity entity) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void insert(UserEntity entity, boolean b) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public UserEntity update(UserEntity entity) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public UserEntity update(UserEntity entity, boolean b) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void delete(String s) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void delete(UserEntity entity) {
        throw new RuntimeException("this is not method!");
    }

    @Override
    public void delete(UserEntity entity, boolean b) {
        throw new RuntimeException("this is not method!");
    }

    /*@Override
    public UserEntity findUserById(String userId) {
        SysUser sysUser = new SysUser();
        sysUser.setLoginName(userId);
        List<SysUser> sysUserList = sysUserMapper.selectUserList(sysUser);
        if (sysUserList != null && sysUserList.size() > 0) {
            return ActivitiUtil.toActivitiUser(sysUserList.get(0));
        }
        return null;//返回的是activiti的实体类
    }

    @Override
    public List<Group> findGroupsByUser(final String userCode) {
        if (userCode == null)
            return null;
        SysUser sysUser = new SysUser();
        sysUser.setLoginName(userCode);
        List<SysUser> sysUserList = sysUserMapper.selectUserList(sysUser);
        if (sysUserList != null && sysUserList.size() > 0) {
            return ActivitiUtil.toActivitiGroups(sysUserList.get(0).getRoles());
        }
        return null;
    }*/

}
